import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
const app = getApp();
// component/personnel-management/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  // 组件生命周期函数-在组件实例进入页面节点树时执行)
  attached() {
    this.getData();
  },

  /**
   * 组件的初始数据
   */
  data: {
    id: "",
    name: "",
    showInput: false,
    showStatus: "添加",
    list: [{}, {}, {}]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取物料列表
    getData() {
      wx.showLoading({
        title: "获取中..."
      });
      wx.$apis.getMateriel(app.globalData.userInfo.companyData.companyId).then((res) => {
        wx.stopPullDownRefresh();
        if (res.data) {
          this.setData({
            list: res.data
          });
        }
      });
    },
    // 修改物料名称
    onEdit(e) {
      let id = e.target.dataset.id;
      let name = e.target.dataset.name;
      this.setData({
        showStatus: "修改",
        showInput: true,
        id,
        name
      });
    },
    // 关闭重置数据
    onCancel() {
      this.setData({
        showStatus: "添加",
        showInput: false,
        id: "",
        name: ""
      });
    },
    onSubmit() {
      let api = "addMateriel";
      if (this.data.showStatus === "修改") {
        api = "editMateriel";
      }
      wx.$apis[api]({
        companyId: app.globalData.userInfo.companyData.companyId,
        id: this.data.id,
        name: this.data.name
      }).then((res) => {
        this.onCancel();
        this.getData();
        wx.showToast({
          title: "修改成功",
          icon: "success",
          duration: 1000
        });
      });
    },
    toAddMaterial() {
      this.setData({
        showInput: true
      });
    },
    delete(id) {
      wx.showLoading({
        title: "删除中，请稍后..."
      });
      wx.$apis.deleteMateriel(id).then((res) => {
        wx.showToast({
          title: "删除成功",
          icon: "success",
          duration: 1000
        }).then(() => {
          setTimeout(() => {
            this.getData();
          }, 1000);
        });
      });
    },
    onDelete(event) {
      let id = event.target.dataset.id;
      Dialog.confirm({
        message: "确定删除吗？"
      }).then(() => {
        this.delete(id);
      });
    },
    onPullDownRefresh() {
      this.getData();
      setTimeout(() => {
        wx.stopPullDownRefresh();
      }, 1000);
    },
    onReachBottom() {
      // this.getData()
    }
  }
});
